from madin import *
import paras
import pandas
import random
from torchRnnShow import getrnn
from mongoapi import finddb2


#join_data  data1 usdjpy data2 audjpy
def compares_data(data1,data2):
    data1time = [a['_id'] for a in data1]
    data2time = [a['_id'] for a in data2]
    margelist = list(set(data1time).intersection(set(data2time)))
    data1copy = data1[::]
    data2copy = data2[::]
    for i in range(len(data1copy)):
        if data1copy[i]["_id"] not in margelist:
            data1.remove(data1copy[i])
    for i in range(len(data2copy)):
        if data2copy[i]["_id"] not in margelist:
            data2.remove(data2copy[i])
    return data1,data2


def getdatas(dbname="FX", symbol='FXUSDJPY', period=5):
    datalist = finddb2(dbname=dbname, tablename=symbol, limit=200000)
    datalist = joinKline(datalist, period)

    signals = getrnn(datalist)
    datalist = datalist[200:]

    rows = []
    index = []
    for i in range(len(datalist)):
        data = datalist[i]
        index.append(datetime.datetime.fromtimestamp(data['Date']))
        rows.append(
            [
                data['Open'],
                data['High'],
                data['Low'],
                data['Close'],
                signals[i],
                random.uniform(1, 10),
            ]
        )

    df = pandas.DataFrame(rows, index=index, columns=['open', 'high', 'low', 'close', 'volume', 'openinterest'])
    df.to_csv("data/{}_{}.csv".format(symbol, period))
    print('save csv file succ')


if __name__ == '__main__':
    symbol = "FXUSDJPY"
    # symbol = "sz50_1"
    getdatas(paras.dbname, paras.symbol, paras.qt_type)
